package xyz.xiaomuyu.moment_read_app.activity;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import xyz.xiaomuyu.moment_read_app.MyTabbar;
import xyz.xiaomuyu.moment_read_app.R;


public class Music extends Activity implements View.OnClickListener {
    static boolean firstPlay = true;
    // 获取界面中显示歌曲标题、作者文本框
    TextView title, author;
    // 播放/暂停、停止按钮
    ImageButton play, next, last;
    // 声明音量管理器
    public AudioManager mAudioManager = null;
    // 定义进度条
    public static SeekBar audioSeekBar = null;
    // 定义音量大小
    public SeekBar audioVolume = null;

    xyz.xiaomuyu.moment_read_app.activity.Music.ActivityReceiver activityReceiver;

    public static final String CTL_ACTION = "org.crazyit.action.CTL_ACTION";

    public static final String UPDATE_ACTION = "org.crazyit.action.UPDATE_ACTION";
    // 定义音乐的播放状态 ，0x10代表首次播放 0x11代表没有播放；0x12代表正在播放；0x13代表暂停
    static int status = 0x10;



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_home_page_music);
        ImageView imageViewmusicreturnhome=(ImageView)findViewById(R.id.musicrehome);
        imageViewmusicreturnhome.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Music.this, MyTabbar.class);
                startActivity(intent);
            }
        });


//        requestWindowFeature(Window.FEATURE_NO_TITLE);


        // 获取程序界面界面中的两个按钮
        play = (ImageButton) this.findViewById(R.id.play);

//        audioVolume = (SeekBar) findViewById(R.id.seekBar2);
        audioSeekBar = (SeekBar) findViewById(R.id.seekBar1);
        // 为两个按钮的单击事件添加监听器
        play.setOnClickListener(this);


        // 播放进度监听
        audioSeekBar.setOnSeekBarChangeListener(new xyz.xiaomuyu.moment_read_app.activity.Music.SeekBarChangeEvent());
        // 退出后再次进去程序时，进度条保持持续更新
        if (FragmentHomeMusicService.mPlayer != null) {
            // 设置进度条的最大值
            xyz.xiaomuyu.moment_read_app.activity.Music.audioSeekBar.setMax(FragmentHomeMusicService.mPlayer.getDuration());
            audioSeekBar.setProgress(FragmentHomeMusicService.mPlayer.getCurrentPosition());
        }




        // 创建IntentFilter
        IntentFilter filter = new IntentFilter();
        // 指定BroadcastReceiver监听的Action
        filter.addAction(UPDATE_ACTION);
        // 注册BroadcastReceiver
        registerReceiver(activityReceiver, filter);







    }

    // 音量监听
    class AudioVolumeChangeEvent implements SeekBar.OnSeekBarChangeListener {

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress,
                                      boolean fromUser) {
            mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, progress,
                    0);
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }

    }

    // 播放进度监听，别忘了Service里面还有个进度条刷新
    class SeekBarChangeEvent implements SeekBar.OnSeekBarChangeListener {

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            // 假设改变源于用户拖动
            if (fromUser) {
                FragmentHomeMusicService.mPlayer.seekTo(progress);
                // 当进度条的值改变时，音乐播放器从新的位置开始播放
            }
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            FragmentHomeMusicService.mPlayer.pause();
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            FragmentHomeMusicService.mPlayer.start();
        }

    }


    // 自定义的BroadcastReceiver，负责监听从Service传回来的广播
    public class ActivityReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            // 获取Intent中的update消息，update代表播放状态
//            int update = intent.getIntExtra("update", -1);
//            // 获取Intent中的current消息，current代表当前正在播放的歌曲
//            int current = intent.getIntExtra("current", -1);


//            switch (update) {
//                case 0x11:
//                    play.setImageResource(R.drawable.play);
//                    status = 0x11;
//                    break;
//                // 控制系统进入播放状态
//                case 0x12:
//                    // 播放状态下设置使用暂停图标
//                    play.setImageResource(R.drawable.pause);
//                    // 设置当前状态
//                    status = 0x12;
//                    break;
//                // 控制系统进入暂停状态
//                case 0x13:
//                    // 暂停状态下设置使用播放图标
//                    play.setImageResource(R.drawable.play);
//                    // 设置当前状态
//                    status = 0x13;
//                    break;
//            }
        }
    }

    @Override
    public void onClick(View source) {
        // 创建Intent
        Intent intent = new Intent("org.crazyit.action.CTL_ACTION");
        if(status == 0x10) {
            Intent intent1 = new Intent(Music.this,FragmentHomeMusicService.class);
            // 启动后台Service
            startService(intent1);
            play.setImageResource(R.drawable.pause);
//            intent.putExtra("status", status);
            status = 0x12;
        }
        else if(status == 0x11){
            play.setImageResource(R.drawable.play);
            intent.putExtra("status", status);
            status = 0x12;
            sendBroadcast(intent);
        }
        else if(status == 0x12){
            play.setImageResource(R.drawable.pause);
            intent.putExtra("status", status);
            status = 0x11;
            sendBroadcast(intent);
        }

//        switch (source.getId()) {
//            // 按下播放/暂停按钮
//            case R.id.play:
//
//                break;
//            case R.id.last:
//                intent.putExtra("control", 2);
//                break;
//            //按下上一首按钮
//            case R.id.next:
//                intent.putExtra("control", 3);
//                break;
//        }
        // 发送广播，将被Service组件中的BroadcastReceiver接收到

    }


}





//
//public class Music extends Activity {
//    @Override
//    public void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.music);
//        ImageView imageViewmusicreturnhome=(ImageView)findViewById(R.id.musicrehome);
//        imageViewmusicreturnhome.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent intent = new Intent(Music.this, MyTabbar.class);
//                startActivity(intent);
//            }
//        });
////        ImageView imageViewmore=(ImageView)findViewById(R.id.more);
////        imageViewmore.setOnClickListener(new View.OnClickListener() {
////            @Override
////            public void onClick(View v) {
////                Intent intent = new Intent(com.example.practice.Music.this,MusicFragment.class);
////                startActivity(intent);
////            }
////        });
//    }
//}
